package org.wewar;

import java.awt.Color;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class UniToGb {
	private static JFrame frame;
	private static JTextArea txtUni,txtChn;
	private static JButton btnToUni,btnToChn,btnPaste,btnCopy;
	
	public static void main(String args[]){
		frame = new JFrame("\u004E\u0061\u0074\u0069\u0076\u0065\u0026\u0055\u006E\u0069\u0063\u006F" +
				"\u0064\u0065\u0020\u0054\u0072\u0061\u006E\u0073\u006C\u0061\u0074\u006F\u0072\u0020" +
				"\u0062\u0065\u0074\u0061\u0020\u0031\u002E\u0031");
		Container content = frame.getContentPane();
		content.setLayout(null);
		
		//Native
		txtChn = new JTextArea();
		txtChn.setBounds(5, 5, 500, 100);
		txtChn.setLineWrap(true);
		txtChn.setBackground(Color.lightGray);
		//To Unicode
		btnToUni = new JButton("\u0054\u006F\u0020\u0055\u006E\u0069\u0063\u006F\u0064\u0065\u2193");
		btnToUni.setBounds(5, 110, 120, 20);
		btnToUni.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String chnStr = txtChn.getText();
				txtUni.setText(nativeToUnicode(chnStr));
			}
		});
		//To Native
		btnToChn = new JButton("\u0054\u006F\u0020\u004E\u0061\u0074\u0069\u0076\u0065\u2191");
		btnToChn.setBounds(135, 110, 120, 20);
		btnToChn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String uniStr = txtUni.getText();
				try {
					txtChn.setText(unicodeToNative(uniStr));
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(frame, (String)e1.getMessage());
				}
			}
		});
		//Paste
		btnPaste = new JButton("\u0050\u0061\u0073\u0074\u0065\u2191");
		btnPaste.setBounds(335, 110, 80, 20);
		btnPaste.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				txtChn.setText("");
				txtChn.paste();
			}
		});
		//Copy
		btnCopy = new JButton("\u0043\u006F\u0070\u0079\u2193");
		btnCopy.setBounds(425, 110, 80, 20);
		btnCopy.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Toolkit toolkit = Toolkit.getDefaultToolkit();
				Clipboard clipboard = toolkit.getSystemClipboard();
				StringSelection stringSel = new StringSelection(txtUni.getText());
				clipboard.setContents(stringSel, null);
			}
		});
		//Unicode
		txtUni = new JTextArea();
		txtUni.setBounds(5, 135, 500, 100);
		txtUni.setLineWrap(true);
		txtUni.setBackground(Color.PINK);
		
		content.add(txtChn);
		content.add(btnToUni);
		content.add(btnToChn);
		content.add(btnPaste);
		content.add(btnCopy);
		content.add(txtUni);
		
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setSize(520, 270);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(frame.getOwner());
	}
	
	private static String nativeToUnicode(String nativeStr){
		char[] c = nativeStr.toCharArray();
		String resultStr = "";
		for(int i=0;i<c.length;i++){
			int tmpChar = (int)c[i];
			String tmpStr = "";
			tmpStr = Integer.toHexString(tmpChar).toUpperCase();
			if(tmpStr.length()<4){
				resultStr += "\\u00" + tmpStr;
			} else {
				resultStr += "\\u" + tmpStr;
			}
		}
		return resultStr;
	}
	
	private static String unicodeToNative(String unicodeStr) throws Exception{
		char aChar;
		int len = unicodeStr.length();
		StringBuffer outBuffer = new StringBuffer(len);
		for(int x=0;x<len;){
			aChar = unicodeStr.charAt(x++);
			if(aChar == '\\'){
				aChar = unicodeStr.charAt(x++);
				if(aChar == 'u'){
					int value = 0;
					for(int i=0;i<4;i++){
						aChar = unicodeStr.charAt(x++);
						switch(aChar){
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new Exception("Malformed \\uxxxx encoding.");	
						}
					}
					outBuffer.append((char)value);
				}else{
					if(aChar == 't')
						aChar = '\t';
					else if(aChar == 'r')
						aChar = '\r';
					else if(aChar == 'n')
						aChar = '\n';
					else if(aChar == 'f')
						aChar = '\f';
					outBuffer.append(aChar);
				}
			}else{
				outBuffer.append(aChar);
			}
		}
		return outBuffer.toString();
	}
}
